<template>
	<view>
		<view class="setting-list" id="setting-list">

			<view class="text-1" style="margin-top: 8px;" v-if="latitude==0&&longitude==0">当前位置无法定位</view>
			<view class="text-1" style="margin-top: 8px;" v-else>当前位置</view>

			<view style="display: flex;" id='location' v-if="latitude!=0||longitude!=0">
				<view class="cityText" @click="chooselocation" v-if="currentLocationChecked==false">
					<image class="small-icon"
						src="@appstatic/img/my/my_orange_address_icon.png">
						<view class="text-3" style="margin-right: 10px;">
							{{address}}
						</view>
				</view>
				
				<view class="cityText_clicked" @click="chooselocation" v-else>
					<image class="small-icon"
						src="@appstatic/img/my/my_white_address_icon.png">
						<view class="text-4" style="margin-right: 10px;">
							{{address}}
						</view>
				</view>
			</view>
			
			<view style="display: flex;" v-if="isFromPage == '社区'">
				<view class="cityText" @click="chooseCountry" v-if="countryChecked==false">
						<view class="text-3" style="margin-right: 10px;">
							全国
						</view>
				</view>
				<view class="cityText_clicked" @click="chooseCountry" v-else>
						<view class="text-4" style="margin-right: 10px;">
							全国
						</view>
				</view>
			</view>


			<!-- 默认标题头128rpx，定位头长64，确认按钮42px, 全国按钮42px  -->
			
			<view style="position: absolute;height: calc(100% - 64px - 62px - 42px);top: 72px;"
				v-if="latitude==0&&longitude==0">
				<indexAddress @select="select" ref='Clear'></indexAddress>
			</view>

			<view style="position: absolute;height: calc(100% - 64px - 62px - 42px );top: 114px;" v-else>
				<indexAddress @select="select" ref='clear'></indexAddress>
			</view>


			<view id="saveBtn" class="saveBtn_no" @click="goBack_no" v-if="cityClickAble==false">确定</view>
			<view id="saveBtn" class="saveBtn" @click="goBack" v-else>确定</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	const dialog = require("../../../utils/dialog.js");
	const util = require("../../../utils/util.js");
	const network = require("../../../manager/network-manager.js");
	import indexAddress from "../../../components/t-index-address/t-index-address";

	import map from "../../../utils/qqmap-wx-jssdk.min.js"

	export default {
		data() {
			return {
				
				address: '',
				addressCopy: '',
				latitude: 0,
				longitude: 0,
				data: {},
				statusBarHeight: 0, // 状态导航栏高度
				navHeight: 0, // 总体高度
				navigationBarHeight: 0, // 导航栏高度(标题栏高度)
				clickOther: false,
				cityClickAble: false,
				currentLocationChecked:false,
				
				countryChecked:false,
				
				isFromPage:''
			}
		},

		onLoad() {
			this.getLocationFn()
		},
		mounted() {
			var pages = getCurrentPages(); //获取跳转的所有页面
			var nowPage = pages[pages.length - 1]; //当前页
			var prevPage = pages[pages.length - 2]; //上一页
			this.isFromPage=prevPage.$vm.text
		},
		components: {
			indexAddress
		},
		methods: {
			chooseCountry(){
				if (this.clickOther == true) {
					this.data = {}
					// this.reGetLocationFn()
				} else {
					this.data = {}
					this.countryChecked = true
					// this.goBack(this.address)
				}
				this.cityClickAble = true
				//todo 控件数据还原
				this.$refs.clear.clearData();
				this.$refs.Clear.clearData();
			},
			chooselocation: function() {
				if (this.clickOther == true) {
					this.data = {}
					this.reGetLocationFn()
				} else {
					this.currentLocationChecked = true
					// this.goBack(this.address)
				}
				this.cityClickAble = true
				//todo 控件数据还原
				this.$refs.clear.clearData();
				this.$refs.Clear.clearData();

			},
			select(data) {
				this.data = data
				this.cityClickAble = true
				this.clickOther = true
				this.currentLocationChecked = false

			},
			goBack_no: function() {

				uni.showToast({
					title: "请选择城市",
					duration: 3000,
					icon: 'none'
				})
			},
			goBack: function() {
				var data = this.data
				debugger
				console.log(JSON.stringify(data)+"!!!!")
				var province = data.province
				if (province == undefined) {
					province = ''
				}
				var city = data.name
				// if (city == undefined) {
				// 	city = data
				// }

				let pages = getCurrentPages(); //获取跳转的所有页面
				let nowPage = pages[pages.length - 1]; //当前页
				let prevPage = pages[pages.length - 2]; //上一页
				prevPage.$vm //上一页的this
				var area = ''
				
				if (prevPage.$vm.text == '编辑地址'||prevPage.$vm.text =='添加新地址') {
					uni.navigateTo({
						url: '../selectAdministrativeRegion/selectAdministrativeRegion?province=' + province +
							'&city=' + city
					})
				} else if (prevPage.$vm.text == '微信登录') {
					var wechatProvince = province
					prevPage.$vm.province = wechatProvince
					prevPage.$vm.cityCode = data.code
					prevPage.$vm.provinceCode = data.provinceCode
					area = city


					uni.navigateBack({
						delta: 1
					});
				} else if (prevPage.$vm.text == '预约试驾') {
					prevPage.$vm.city = city
					prevPage.$vm.cityCode = data.code
					uni.navigateBack({
						delta: 1
					});
				}else if (prevPage.$vm.text == '订单确认') {
					prevPage.$vm.province = province
					area = city
					uni.navigateBack({
						delta: 1
					});
				}else if(prevPage.$vm.text == '社区'){
					debugger
					prevPage.$vm.swiperCurrent=1
					 // prevPage.$vm.cityName = city
					 // prevPage.$vm.cityCode = data.code
					uni.navigateBack({
					 	delta: 1,
						success: () => {
						    // 调用pageA的refresh方法
						    prevPage.$vm.getCity(city,data.code); 
						  }
					 });
				}else {
					area = city
					prevPage.$vm.province = province
					prevPage.$vm.cityCode = data.code
					prevPage.$vm.provinceCode = data.provinceCode
					prevPage.$vm.fromFunction = "更改地址"
					
					 
					uni.navigateBack({
						delta: 1
					}); //这个是返回上级第一个页面， delta等于2的时候跳过上个页面返回再上一个页面
				}

				prevPage.$vm.area = area



			},
			//个人信息地址保存
			updateInfo: function(province, city) {
				var save_uri = app.globalData.config.interfaces
					.URL_ADMIN_UPDATEUSERINFO;
				var save_params = {
					"id": app.globalData.userInfo.id,
					"province": province,
					"city": city,
				};

				network.put(save_uri, save_params, 'json')
					.then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: res.message,
								duration: 3000,
								icon: 'none'
							})
							app.saveUser(app.globalData.userInfo)


							uni.navigateBack({
								delta: 1
							}); //这个是返回上级第一个页面， delta等于2的时候跳过上个页面返回再上一个页面



						} else {
							uni.showToast({
								title: res.message,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch(res => {
						uni.showToast({
							title: res.errMsg,
							duration: 3000,
							icon: 'none'
						})
						console.error(res);
					});

			},

			//获取地理位置
			getLocationFn: function() {
				let _this = this
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						_this.latitude = res.latitude;
						_this.longitude = res.longitude;
						console.log(_this.latitude + "!!" + _this.longitude);
						var qqmapsdk = new map({
							key: 'SKABZ-LZZW5-VL7IR-IUYJA-L73D6-KMBO6' // 必填，去腾讯地图申请
						});
						qqmapsdk.reverseGeocoder({
							//Object格式
							location: {
								latitude: _this.latitude,
								longitude: _this.longitude
							},
							success: (res) => {
								// _this.cityClickAble = true
								_this.clickOther = false;
								var city = res.result.address_component.city
								_this.addressCopy = city;
								_this.address = city.split("市").join("");

								_this.data.code = res.result.ad_info.adcode.substring(0, 4)
								_this.data.lat = res.result.ad_info.location.lat
								_this.data.lng = res.result.ad_info.location.lng
								_this.data.name = _this.address
								_this.data.province = res.result.address_component.province
								_this.data.provinceCode = res.result.ad_info.adcode.substring(
									0, 2)

							},
							fail: (error) => {
								// console.error(error);
							}
						});
					}
				});


			},


			// 重新获取当前地址
			reGetLocationFn: function() {
				let _this = this
				var qqmapsdk = new map({
					key: 'SKABZ-LZZW5-VL7IR-IUYJA-L73D6-KMBO6' // 必填，去腾讯地图申请
				});
				qqmapsdk.reverseGeocoder({
					//Object格式
					location: {
						latitude: _this.latitude,
						longitude: _this.longitude
					},
					success: (res) => {
						var city = res.result.address_component.city
						_this.addressCopy = city;
						_this.address = city.split("市").join("");
						_this.data.code = res.result.ad_info.adcode.substring(0, 4)
						_this.data.lat = res.result.ad_info.location.lat
						_this.data.lng = res.result.ad_info.location.lng
						_this.data.name = _this.address
						_this.data.province = res.result.address_component.province
						_this.data.provinceCode = res.result.ad_info.adcode.substring(
							0, 2)
						// this.data = this.address
						this.currentLocationChecked = true
						// this.goBack(this.address) 
					},
					fail: (error) => {
						// console.error(error);
					}
				});
			}





		}
	}
</script>

<style>
	@import "./myArea.css";
</style>
